﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Senparc.Weixin;
using Senparc.Weixin.QY;
using Senparc.Weixin.QY.AdvancedAPIs;
using Senparc.Weixin.QY.CommonAPIs;
using Senparc.Weixin.QY.AdvancedAPIs.Mass;

namespace Common.MessageApi
{
    public class File
    {
        /// <summary>
        /// 消息内容如果为文件则上传文件，文本则上传文字
        /// </summary>
        public dynamic data { get; set; }
        /// <summary>
        /// 消息标题
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 消息描述
        /// </summary>
        public string desc { get; set; }
        /// <summary>
        /// 消息链接
        /// </summary>
        public string url { get; set; }

    }
    
    public class Message
    {
        #region 文本测试调用
        public static void sendTextTest()
        {
            List<File> fileList = new List<File>() { };
            var file = new File
            {
                data="测试信息",
                desc="",
                title="",
                url=""
            };
            fileList.Add(file);
            SendMessage(fileList,"","图文");
        }
        #endregion

        #region 发送消息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file">消息内容包含标题，描述，如果是文件类消息则File.data为文件信息，图文则为图片链接，文本则为文本内容</param>
        /// <param name="agentId">企业应用的id，可在应用的设置页面查看</param>
        /// <param name="type">消息类型</param>
        /// <param name="toUser">UserID列表（消息接收者，多个接收者用‘|’分隔）默认全部发送</param>
        /// <param name="toParty">PartyID列表，多个接受者用‘|’分隔。默认为忽略本参数</param>
        /// <param name="toTag">TagID列表，多个接受者用‘|’分隔。默认为忽略本参数</param>
        public static MassResult SendMessage(List<File> file, string agentId, string type, string toUser = "@all", string toParty = "@all", string toTag = "@all")
        {
            var accesstoken = UserApi.User.accesstoken();
            MassResult result = new MassResult();
            var first = file.First();
            if (first == null)
            {
                return null;
            }
            switch (type)
            {
                case "文本":
                    result = MassApi.SendText(accesstoken, toUser, toParty, toTag, agentId, first.data.ToString());
                    break;
                case "图片":
                    result = MassApi.SendImage(accesstoken, toUser, toParty, toTag, agentId, mediaId(accesstoken, UploadMediaFileType.image, first.data));
                    break;
                case "语音":
                    result = MassApi.SendVoice(accesstoken, toUser, toParty, toTag, agentId, mediaId(accesstoken, UploadMediaFileType.voice, first.data));
                    break;
                case "视频":
                    result = MassApi.SendVideo(accesstoken, toUser, toParty, toTag, agentId, mediaId(accesstoken, UploadMediaFileType.video, first.data), first.title, first.desc);
                    break;
                case "文件":
                    result = MassApi.SendFile(accesstoken, toUser, toParty, toTag, agentId, mediaId(accesstoken, UploadMediaFileType.file, first.data));
                    break;
                case "图文":
                    result = MassApi.SendNews(accesstoken, toUser, toParty, toTag, agentId, News(file));
                    break;

            }
            return result;
        }
        #endregion

        #region 填充消息内容
        private static List<Senparc.Weixin.QY.Entities.Article> News(List<File> filelist)
        {
            var articleList = new List<Senparc.Weixin.QY.Entities.Article>() { };
            foreach (var item in filelist)
            {
                var article = new Senparc.Weixin.QY.Entities.Article
                {
                    Description = item.desc,
                    PicUrl = item.data.ToString(),
                    Title=item.title,
                    Url=item.url
                };
                articleList.Add(article);
            }
            return articleList;
        }
        #endregion

        #region 上传图片获取mediaId
        /// <summary>
        /// 上传图片获取mediaId
        /// </summary>
        /// <param name="accesstoken"></param>
        /// <param name="type">上传的格式</param>
        /// <param name="data">form-data中媒体文件标识，有filename、filelength、content-type等信息</param>
        /// <returns></returns>
        private static string mediaId(string accesstoken, UploadMediaFileType type, dynamic data)
        {
            var result = Senparc.Weixin.QY.AdvancedAPIs.MediaApi.Upload(accesstoken, type, data.ToString());
            return result.media_id;
        }
        #endregion

    }

    
}
